package com.uyayo.blog.service.impl;

import com.uyayo.blog.entity.BlogCategory;
import com.uyayo.blog.mapper.BlogCategoryMapper;
import com.uyayo.blog.model.BlogCategoryCondition;
import com.uyayo.blog.service.BlogCategoryService;
import com.uyayo.framework.common.exception.ParentNotFoundException;
import com.uyayo.framework.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;


@Service("blogCategoryService")
public class BlogCategoryServiceImpl extends BaseServiceImpl<BlogCategory> implements BlogCategoryService {

	private final BlogCategoryMapper blogCategoryMapper;

	public BlogCategoryServiceImpl(BlogCategoryMapper blogCategoryMapper) {
		this.blogCategoryMapper = blogCategoryMapper;
	}

	@Override
	public List<BlogCategory> selectBlogCategory(BlogCategoryCondition blogCategoryCondition) {
		Example example = new Example(BlogCategory.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo(blogCategoryCondition);
		example.createCriteria();
		return blogCategoryMapper.selectByExample(example);
	}

	@Override
	public int selectCountByParentId(Long parentId) {
		BlogCategory blogCategory = new BlogCategory();
		blogCategory.setParentId(parentId);
		return blogCategoryMapper.selectCount(blogCategory);
	}

	@Override
	public int saveBlogCategory(BlogCategory blogCategory) throws ParentNotFoundException {
		Long parentId = blogCategory.getParentId();
		if(!Objects.isNull(parentId)){
			BlogCategory blogCategoryParent = selectByKey(parentId);
			if(blogCategoryParent==null){
				throw new ParentNotFoundException("Parent not found!");
			}
		}
		int count = blogCategoryMapper.insertSelective(blogCategory);
		if(count >0){
			updateHasChildrennToParent(parentId);
		}
		return count;
	}

	/**
	 * 迭代检查选择的上级id是否是自己的子级
	 * @param parentIdOld 原上级id
	 * @param parentIdNew	新上级id
	 * @param blogCategoryChild 子资源
	 * @param blogCategoryParent 父资源
	 */
	private void recursiveCheckParentIsChildren(Long parentIdOld, Long parentIdNew, BlogCategory blogCategoryChild, BlogCategory blogCategoryParent){
		if (selectCountByParentId(blogCategoryChild.getId()) > 0) {
			Example example = new Example(BlogCategory.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("parentId",blogCategoryChild.getId());
			List<BlogCategory> blogCategorys = blogCategoryMapper.selectByExample(example);
			if (!blogCategorys.isEmpty()) {
				for(BlogCategory blogCategory:blogCategorys){
					if(Objects.equals(blogCategory.getId(),parentIdNew)){
						blogCategoryParent.setParentId(parentIdOld);
						blogCategoryMapper.updateByPrimaryKeySelective(blogCategoryParent);
					}else {
						recursiveCheckParentIsChildren(parentIdOld, parentIdNew, blogCategory, blogCategoryParent);
					}
				}
			}
		}
	}
	@Override
	public int updateBlogCategory(BlogCategory blogCategory) throws ParentNotFoundException {

		BlogCategory blogCategoryOld = selectByKey(blogCategory.getId());

		if(blogCategory.getParentId()==null){
			blogCategory.setParentId(0L);
		}

		Long parentId = blogCategory.getParentId();
		if(parentId!=0){

			//获取修改的父级资源
			BlogCategory blogCategoryParent = selectByKey(parentId);
			if(blogCategoryParent==null){
				throw new ParentNotFoundException("Parent not found!");
			}
			if (selectCountByParentId(blogCategoryOld.getId()) > 0) {
				Example example = new Example(BlogCategory.class);
				Example.Criteria criteria = example.createCriteria();
				criteria.andEqualTo("parentId",blogCategoryOld.getId());
				List<BlogCategory> blogCategorys = blogCategoryMapper.selectByExample(example);
				if (!blogCategorys.isEmpty()) {
					for(BlogCategory blogCategory_:blogCategorys){
						if(Objects.equals(blogCategory_.getId(),parentId)){
							blogCategoryParent.setParentId(blogCategoryOld.getParentId());
							blogCategoryMapper.updateByPrimaryKeySelective(blogCategoryParent);
						}else{
							recursiveCheckParentIsChildren(blogCategoryOld.getParentId(),blogCategory.getParentId(),blogCategory,blogCategoryParent);
						}
					}
				}
			}
		}
		if(selectCountByParentId(blogCategory.getId())>0) {
			blogCategory.setHasChildren(true);
		}else{
			blogCategory.setHasChildren(false);
		}
		int count = blogCategoryMapper.updateByPrimaryKeySelective(blogCategory);
		if(count<=0){
			return count;
		}
		updateHasChildrennToParent(parentId);
		if(blogCategoryOld!=null){
			updateHasChildrennToParent(blogCategoryOld.getParentId());
		}
		return count;
	}

	/**
	 * 修改父资源是否含子资源
	 * @param parentId 父资源id
	 */
	private void  updateHasChildrennToParent(Long parentId){
		if(!Objects.isNull(parentId)){
			BlogCategory blogCategory = new BlogCategory();
			blogCategory.setId(parentId);
			if(selectCountByParentId(parentId)>0) {
				blogCategory.setHasChildren(true);
			}else{
				blogCategory.setHasChildren(false);
			}
			blogCategoryMapper.updateByPrimaryKeySelective(blogCategory);
		}
	}

	@Override
	public int deleteBlogCategory(Long id) {

		if (selectCountByParentId(id) > 0) {
			Example example = new Example(BlogCategory.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("parentId",id);
			List<BlogCategory> blogCategorys = blogCategoryMapper.selectByExample(example);
			if (!blogCategorys.isEmpty()) {
				for(BlogCategory blogCategory:blogCategorys){
					recursiveDelete( blogCategory);
				}
			}
		}
		return delete(id);
	}

	/**
	 * 递归删除资源
	 * @param blogCategory 资源
	 */
	private void recursiveDelete(BlogCategory blogCategory){

		if (selectCountByParentId(blogCategory.getId()) > 0) {
			Example example = new Example(BlogCategory.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("parentId",blogCategory.getId());
			List<BlogCategory> blogCategorys = blogCategoryMapper.selectByExample(example);
			if (!blogCategorys.isEmpty()) {
				for(BlogCategory blogCategoryChild:blogCategorys){
					recursiveDelete(blogCategoryChild);
				}
			}
		}
		delete(blogCategory.getId());
	}

	public int delete(Long id) {
		BlogCategory blogCategory = blogCategoryMapper.selectByPrimaryKey(id);
		int count = blogCategoryMapper.deleteByPrimaryKey(id);
		if(count<=0){
			return count;
		}
		updateHasChildrennToParent(blogCategory!=null?blogCategory.getParentId():null);
		return count;
	}
}
